import Vue from 'vue'
import router from '@/router'
import axios from 'axios';
import Qs from 'qs';
import { resolve } from 'path';

var developmentUrl = process.env.BASE_API;

class Http {};
Http.install = function (Vue, options) {
    // reset本地储存，并返回首页
    function resetLocalStorage(){
        localStorage.removeItem('username');
        localStorage.removeItem('token');
        router.push('/');
    }
    // 获取第三方数据
    function getThirdPartyData(url,data){
        var token = '';

        return new Promise(function(resolve,reject){
            axios({
                url: url,
                method: 'get', // 默认是 get
                baseURL: '',
                headers: {
                    // 'Content-Type': 'multipart/form-data' ,
                    'Content-Type': 'application/json' ,
                },
                data: JSON.stringify(data)
            }).then((res)=>{
                resolve(res);
            }).catch((err)=>{
                reject(err);
                console.log(err);
            });
        })
    }
    // get的方法
    function get(url,data){
        var token = '';

        if(localStorage.getItem('token')){
            token = localStorage.getItem('token');
        }
        return new Promise(function(resolve,reject){
            axios({
                url: url,
                method: 'get', // 默认是 get
                baseURL: developmentUrl,
                headers: {
                    // 'Content-Type': 'multipart/form-data' ,
                    'Content-Type': 'application/json' ,
                    // "token":token,
                },
                data: JSON.stringify(data)
            }).then((res)=>{
                if(res.data.meta.code == 200){
                    resolve(res);
                }
                if(res.data.meta.code == 401){
                    exit({
                        "token":token,
                    });
                }
                else{
                    reject(res);
                }
            }).catch((err)=>{
                reject(err);
                console.log(err);
            });
        })
    }
    // post的方法
    function post(url,data){
        var token = '';

        if(localStorage.getItem('token')){
            token = localStorage.getItem('token');
        }
        return new Promise(function(resolve,reject){
            axios({
                url: url,
                method: 'post', 
                baseURL: developmentUrl,
                headers: { 
                    // 'Content-Type': 'multipart/form-data' ,
                    'Content-Type': 'application/json' ,
                    // "token":token,
                },
                data: JSON.stringify(data)
            }).then((res)=>{
                if(res.data.code == 200){
                    resolve(res);
                }
                else{
                    reject(res);
                }
            }).catch((err)=>{
                reject(err);
            });
        })
    }

    // post的方法
    function exit(data){
        var token = '';

        if(localStorage.getItem('token')){
            token = localStorage.getItem('token');
        }
        return new Promise(function(resolve,reject){
            axios({
                url: '/logout',
                method: 'post', 
                baseURL: developmentUrl,
                headers: { 
                    // 'Content-Type': 'multipart/form-data' ,
                    'Content-Type': 'application/json' ,
                    // "token":token,
                },
                data: JSON.stringify(data)
            }).then((res)=>{
                if(res.data.meta.code == 200){
                    localStorage.removeItem('username');
                    localStorage.removeItem('token');
                    window.location.href = 'https://sso.zenmen.com/cas/logout?service=http://jiagu.wifi.com';
                }
            }).catch((err)=>{
                reject(err);
            });
        })
    }
    // vue调用get的方法
    Vue.prototype.$getThirdPartyData = function(url,data){
        return getThirdPartyData(url,data);
    }
    // vue调用get的方法
    Vue.prototype.$get = function(url,data){
        return get(url,data);
    }
    // vue调用post的方法
    Vue.prototype.$post = function(url,data){
        return post(url,data);
    }
}

Vue.use(Http);
export default Http;